// 和用户相关的状态管理

import { createSlice } from '@reduxjs/toolkit'
import { request } from '@/utils'

const userStore = createSlice({
  name:'user',
  initialState:{
    token: '',
  },
  reducers:{
    setToken(state,action){
      state.token = action.payload
    },
  }
})

const { setToken } = userStore.actions

const userReducer = userStore.reducer

// 异步方法 完成登录获取token
const fetchLogin = (loginForm) => {
  return async (dispatch)=>{
    const res = await request.post('/authorizations', loginForm);
    dispatch(setToken(res.data.token))
  }
}

export { fetchLogin, setToken }

export default userReducer